<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Shop extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'shop';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'image_str',
        'order_num',
        'status_text',
        'state_text'
    ];



    public function getImageStrAttr($value, $data)
    {
        return $data['image'] ? cdnurl($data['image'], true) : '';
    }
    public function getStatusList()
    {
        return ['normal' => __('Status normal'), 'hidden' => __('Status hidden')];
    }
    public function getStateList()
    {
        return ['0' => __('审核中'), '1' => __('审核通过'), '2' => __('审核失败')];
    }
    public function getOrderNumAttr($value, $data)
    {

        return Order::where('shop_id', $data['id'])->where('status', 2)->count();
    }
    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function user()
    {
        return $this->hasMany('app\common\model\User', 'shop_id', 'id');
    }
}
